<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.keyword"
        placeholder="请输入关键字"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter="handleFilter"
      />
      <el-select
        v-model="listQuery.status"
        placeholder="状态"
        clearable
        style="width: 120px"
        class="filter-item"
      >
        <el-option
          v-for="item in statusOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-date-picker
        v-model="listQuery.dateRange"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        class="filter-item"
        style="width: 360px"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column
        label="流程名称"
        prop="processName"
        align="center"
        width="200"
      />
      <el-table-column
        label="当前节点"
        prop="currentNode"
        align="center"
        width="150"
      />
      <el-table-column
        label="处理人"
        prop="handler"
        align="center"
        width="120"
      />
      <el-table-column
        label="开始时间"
        prop="startTime"
        align="center"
        width="180"
      />
      <el-table-column
        label="状态"
        prop="status"
        align="center"
        width="100"
      >
        <template #default="{row}">
          <el-tag :type="getStatusType(row.status)">
            {{ getStatusText(row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="150"
      >
        <template #default="{row}">
          <el-button
            size="mini"
            type="primary"
            @click="handleDetail(row)"
          >
            查看详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="listQuery.page"
      v-model:limit="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'

export default {
  name: 'ProcessMonitor',
  components: { Pagination },
  directives: { waves },
  setup() {
    const list = ref(null)
    const total = ref(0)
    const listLoading = ref(true)
    const listQuery = reactive({
      page: 1,
      limit: 20,
      keyword: undefined,
      status: undefined,
      dateRange: []
    })

    const statusOptions = [
      { label: '进行中', value: 'processing' },
      { label: '已完成', value: 'completed' },
      { label: '已终止', value: 'terminated' }
    ]

    const getStatusType = (status) => {
      switch (status) {
        case 'processing':
          return 'warning'
        case 'completed':
          return 'success'
        case 'terminated':
          return 'danger'
        default:
          return 'info'
      }
    }

    const getStatusText = (status) => {
      switch (status) {
        case 'processing':
          return '进行中'
        case 'completed':
          return '已完成'
        case 'terminated':
          return '已终止'
        default:
          return '未知'
      }
    }

    const getList = () => {
      listLoading.value = true
      // TODO: 调用API获取流程监控列表
      setTimeout(() => {
        list.value = []
        total.value = 0
        listLoading.value = false
      }, 1000)
    }

    const handleFilter = () => {
      listQuery.page = 1
      getList()
    }

    const handleDetail = (row) => {
      // TODO: 实现查看流程详情功能
    }

    onMounted(() => {
      getList()
    })

    return {
      list,
      total,
      listLoading,
      listQuery,
      statusOptions,
      getStatusType,
      getStatusText,
      handleFilter,
      handleDetail
    }
  }
}
</script>